package com.jjb.www.ui.fragment;

import android.view.View;

import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.ViewPager;

import com.hjq.base.FragmentPagerAdapter;
import com.hjq.http.EasyHttp;
import com.hjq.http.listener.HttpCallback;
import com.hjq.widget.layout.NestedViewPager;
import com.jjb.www.R;
import com.jjb.www.aop.Permissions;
import com.jjb.www.aop.SingleClick;
import com.jjb.www.app.AppFragment;
import com.jjb.www.app.TitleBarFragment;
import com.jjb.www.http.api.CurriculumSortApi;
import com.jjb.www.http.api.StaydoApi;
import com.jjb.www.http.glide.GlideApp;
import com.jjb.www.http.model.HttpList;
import com.jjb.www.ui.activity.HistoryListActivity;
import com.jjb.www.ui.activity.HomeActivity;
import com.jjb.www.ui.adapter.TabAdapter;
import com.jjb.www.ui.adapter.TabBoldAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * author : Android CRG
 * github : https://github.com/getActivity/AndroidProject
 * time   : 2018/10/18
 * desc   : 学习 Fragment
 */
public final class StudyFragment extends TitleBarFragment<HomeActivity> implements TabBoldAdapter.OnTabListener, ViewPager.OnPageChangeListener {

    private RecyclerView mTabView;
    private NestedViewPager mViewPager;
    private FragmentPagerAdapter<AppFragment<?>> mPagerAdapter;
    private TabBoldAdapter mTabAdapter;

    public static StudyFragment newInstance() {
        return new StudyFragment();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.message_fragment;
    }

    @Override
    protected void initView() {
        mTabView = findViewById(R.id.rv_study_tab);
        mViewPager = findViewById(R.id.vp_study_pager);

        mPagerAdapter = new FragmentPagerAdapter<>(this);

//        mPagerAdapter.addFragment(StudyListJiNengFragment.newInstance(), "特种作业人员");

        mViewPager.addOnPageChangeListener(this);

        mTabAdapter = new TabBoldAdapter(getAttachActivity(), TabAdapter.TAB_MODE_DESIGN, false);
        mTabView.setAdapter(mTabAdapter);
    }

    @Override
    protected void initData() {

        mTabAdapter.setOnTabListener(this);

    }

    @Override
    public void onResume() {
        super.onResume();
        getStydo();
    }

    @Override
    public void onRightClick(View view) {
        startActivity(HistoryListActivity.class);
    }

    private void getStydo() {
        EasyHttp.get(this)
                .api(new StaydoApi())
                .request(new HttpCallback<HttpList<StaydoApi.Bean>>(this) {

                    @Override
                    public void onSucceed(HttpList<StaydoApi.Bean> data) {
                        mPagerAdapter.clearFragment();
                        mTabAdapter.clearData();
                        mViewPager.setAdapter(mPagerAdapter);
                        for (StaydoApi.Bean bean : data.getData()) {

                            if (bean.getType() == 2) {
                                //继教

                                mPagerAdapter.addFragment(StudyListFragment.newInstance(bean.getType(), bean.getCertType() + ""), bean.getLabelV3());
                                mTabAdapter.addItem(bean.getLabelV3());
                            } else if (bean.getType() == 3) {
                                //岗前
                                mPagerAdapter.addFragment(StudyListJiNengFragment.newInstance(bean.getId()+"",bean.getType() + "", bean.getPlanType()), bean.getLabelV3());
                                mTabAdapter.addItem(bean.getLabelV3());
                            }

                        }


                        for (int i = 0; i < mTabAdapter.getCount(); i++) {
                            if (HomeActivity.FRAGMENT_TYPE==i){
                                mTabAdapter.setSelectedPosition(i);
                                int finalI = i;
                                postDelayed(() -> {
                                    mTabView.smoothScrollToPosition(finalI);

                                }, 300);
//
                                mViewPager.setCurrentItem(i);
                            }
                        }
                    }

                });
    }

    @Override
    public void onPause() {
        super.onPause();
        HomeActivity.FRAGMENT_TYPE = mTabAdapter.getSelectedPosition();
    }

    @Override
    public boolean isStatusBarEnabled() {
        // 使用沉浸式状态栏
        return !super.isStatusBarEnabled();
    }

    @SingleClick
    @Override
    public void onClick(View view) {
        int viewId = view.getId();
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        if (mTabAdapter == null) {
            return;
        }
        mTabAdapter.setSelectedPosition(position);
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    @Override
    public boolean onTabSelected(RecyclerView recyclerView, int position) {
        mViewPager.setCurrentItem(position);
        return true;
    }
}